package com.doit;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @DATE 2021/12/10/16:18
 * @Author MDK
 * @Version 2021.2.2
 **/
public class C05_Demo05 {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("linux01:2181,linux03:2181,linux02:2181",1500,null);
        //判断节点是否存在
        Stat exists = zk.exists("/aaa", null);
        if(exists != null){
            rmr("/aaa",zk);
        }
        zk.close();
    }

    public static void rmr(String path, ZooKeeper zk) throws Exception{
        List<String> list = zk.getChildren(path, null);
        //判断是否存在子节点
        if(list != null && list.size()>0){
            for (String name : list) {
                rmr(path+"/"+name,zk);
            }
        }
        zk.delete(path,-1);
    }
}
